package com.szyportal.web.back.controller.tool;

import com.szyportal.common.core.controller.BaseController;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import org.thymeleaf.TemplateEngine;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

/**
 * http://127.0.0.1/back/tool/generStaticHtml
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/back/tool")
public class StaticPageGeneratorController
{
    @GetMapping("/generStaticHtml")
    public String generStaticHtml()
    {

        //path - fileName
        HashMap<String,String> pageMap = new HashMap<>();
        pageMap.put("/","index.html");
        pageMap.put("/about.html","about.html");
        pageMap.put("/service","service.html");
        pageMap.put("/case?pageSize=9","case.html");
        pageMap.put("/partner","partner.html");
        pageMap.put("/news","news.html");
        pageMap.put("/contact","contact.html");

        String outputDir = "/Users/gds/project2025/szyPortalV2/websites/szyTech/";
        String apiUrl = "http://127.0.0.1:80";
//        String fileName = "/index.html";
        RestTemplate restTemplate = new RestTemplate();
        pageMap.forEach((path,fileName) -> {
            try {
                // 调用API
                String apiResponse = restTemplate.getForObject(apiUrl+path, String.class);
                // 保存文件
                Path outputPath = Paths.get(outputDir);
                if (!Files.exists(outputPath)) {
                    Files.createDirectories(outputPath);
                }
                Path filePath = outputPath.resolve(fileName);
                Files.write(filePath, apiResponse.getBytes());

                System.out.println("HTML文件已保存到: " + filePath);
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
        return "success";
    }

}
